package figure8;

import java.io.Serializable;

public class LoopTiming implements Serializable {
	private static final long serialVersionUID = -6978181738644007845L;
	
	private final int tempo; // Tempo in BPM (beats per minute)
    private final int bars; // The number of bars in each loop
    private final int beats; // The number of beats in each bar
    private final int note; // The length of the note (4 = 1s)
    private final double length; // The length of this loop in seconds

    public LoopTiming(int tempo, int bars, int beats, int note) {
        this.tempo = tempo;
        this.bars = bars;
        this.beats = beats;
        this.note = note;

        // Just a little proof that the time calculations produce the right
        // unit:
        // Tempo in BPM is (beats / time)
        // (time / (beats / time)) is (time**2 / beats)
        // ((time**2 / beats) * beats) is (time**2)
        // time = (time / (beats / time)) * (beats / bar) * (1 / time) * bars
        // time = (time**2 / beats) * (beats / bar) * bars * (1 / time)
        // time = (time**2 / beats) * beats * (1 / time)
        // time = time**2 * (1 / time)
        // time = time
        this.length = (60.0 / this.tempo) * this.beats * (4.0 / this.note) * this.bars;
    }

    public int getTempo() {
        return this.tempo;
    }

    public int getBars() {
        return this.bars;
    }

    public int getBeats() {
        return this.beats;
    }

    public int getNote() {
        return this.note;
    }

    public double getLength() {
        return this.length;
    }
    
    public double getBarLength() {
    	return this.length / this.bars;
    }
    
    public double getBeatLength() {
    	return (1000 * this.length) / (double)(this.bars * this.beats);
    }
    
    public String toString() {
        StringBuilder output = new StringBuilder();

        output.append("    Tempo (bpm): " + tempo + "\n");
        output.append("    Bars per loop: " + bars + "\n");
        output.append("    Beats per bar: " + beats + "\n");
        output.append("    Note length (unknown unit): " + note + "\n");
        output.append("    Total length (s): " + this.length + "\n");

        return output.toString();
    }
}
